package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.PersonnelManagement;
import com.example.prison.service.PersonnelManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("人员管理")
@RestController
@RequestMapping("/api")
public class PersonnelManagementController {

    @Autowired
    private PersonnelManagementService personnelManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取推送人员列表")
    @GetMapping("/getPersonnel")
    public Response<PageInfo<PersonnelManagement>> getPushPersonnel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取推送人员列表");
        PageInfo<PersonnelManagement> pageInfo = personnelManagementService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取哨卫人员列表")
    @GetMapping("/getSentryPersonnel")
    public Response<PageInfo<PersonnelManagement>> getSentryPersonnel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取哨卫人员列表");
        PageInfo<PersonnelManagement> pageInfo = personnelManagementService.getSentry(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取全部人员列表")
    @GetMapping("/getAllPersonnel")
    public Response<PageInfo<PersonnelManagement>> getAllPersonnel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取全部人员列表");
        PageInfo<PersonnelManagement> pageInfo = personnelManagementService.getAll(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增人员")
    @PostMapping("/addPersonnel")
    public Response<?> addPersonnel(@RequestBody PersonnelManagement personnel) {
        log.info("新增人员");
        systemLogService.add("新增人员");
        personnel.setIsPushPeople(1);
        int result = personnelManagementService.add(personnel);
        if (result == -1) {
            return Response.error("人员已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("新增推送人员")
    @PostMapping("/addPushPersonnel")
    public Response<?> addPushPersonnel(@RequestBody PersonnelManagement personnel) {
        log.info("新增推送人员");
        systemLogService.add("新增推送人员");
        personnel.setIsPushPeople(1);
        int result = personnelManagementService.add(personnel);
        if (result == -1) {
            return Response.error("人员已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("新增哨卫")
    @PostMapping("/addSentryPersonnel")
    public Response<?> addSentryPersonnel(@RequestBody PersonnelManagement personnel) {
        log.info("新增哨卫");
        systemLogService.add("新增哨卫");
        personnel.setIsSentry(1);
        int result = personnelManagementService.add(personnel);
        if (result == -1) {
            return Response.error("人员已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除人员")
    @PostMapping("/deletePersonnel")
    public Response<?> deletePersonnel(@Parameter(description = "人员ID") @RequestBody Integer[] ids) {
        log.info("删除人员");
        systemLogService.add("删除人员");
        personnelManagementService.delete(ids);
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑人员")
    @PostMapping("/updatePersonnel")
    public Response<?> updatePersonnel(@RequestBody PersonnelManagement personnel) {
        log.info("编辑人员");
        systemLogService.add("编辑人员");
        Integer id = personnel.getId();
        int result = personnelManagementService.update(personnel, id);
        if (result == -1) {
            return Response.error("更新失败！人员已存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据id获取人员列表")
    @GetMapping("/getPersonnelById")
    public Response<PersonnelManagement> getPersonnelById(@Parameter(description = "人员ID")@RequestParam(value = "personnelId") Integer personnelId) {
        log.info("依据id获取人员列表");
        systemLogService.add("依据id获取人员列表");
        PersonnelManagement personnel = personnelManagementService.getById(personnelId);
        if(ObjectUtils.isEmpty(personnel)) {
            return Response.error("此人员id不存在！");
        }
        return Response.success("获取成功！", personnel);
    }

    @ApiOperation("依据姓名获取人员")
    @GetMapping("/getPersonnelByName")
    public Response<PageInfo<PersonnelManagement>> getPersonnelByName(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @Parameter(description = "人员名")@RequestParam(value = "personnelName") String personnelName) {
        log.info("依据姓名获取人员");
        systemLogService.add("依据姓名获取人员");
        PageInfo<PersonnelManagement> pageInfo = personnelManagementService.getByName(pageNumber, pageSize, personnelName);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此人员名不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
